package com.hjms.tenant.services;

import cn.hutool.core.date.DateUtil;
import com.hjms.common.entity.exception.Precondition412Exception;
import com.hjms.tenant.contracts.IEmployeeService;
import com.hjms.tenant.contracts.ITenantService;
import com.hjms.tenant.contracts.dto.tenant.AddTenantDto;
import com.hjms.tenant.contracts.vo.tenant.TenantVo;
import com.hjms.tenant.domain.Employee;
import com.hjms.tenant.domain.EmployeeRole;
import com.hjms.tenant.domain.Role;
import com.hjms.tenant.domain.Tenant;
import com.hjms.tenant.repository.EmployeeMapper;
import com.hjms.tenant.repository.EmployeeRoleMapper;
import com.hjms.tenant.repository.RoleMapper;
import com.hjms.tenant.repository.TenantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.UUID;

/**
 * @author 何杨谊
 * @description: TODO
 * @date 2023/1/2 13:36
 */
@Service
public class TenantService implements ITenantService {
    @Resource
    private TenantMapper tenantMapper;
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private EmployeeRoleMapper employeeRoleMapper;
    @Resource
    private RoleMapper roleMapper;
    @Autowired
    private IEmployeeService employeeService;

    @Override
    public TenantVo Get(String tenantId) {
        Tenant entity=tenantMapper.selectById(tenantId);

        // 组装返回租户信息
        TenantVo vo=new TenantVo();
        vo.setId(entity.getId());
        vo.setName(entity.getName());
        return vo;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public String Add(AddTenantDto dto) {
        // 组装Id准备
        String tenantId = UUID.randomUUID().toString();
        String roleId = UUID.randomUUID().toString();
        String employeeId = UUID.randomUUID().toString();

        // 添加租户信息
        Tenant tenant = new Tenant();
        tenant.setId(tenantId);
        tenant.setName(dto.getTenantName());
        tenant.setCreateEmployee(employeeId);
        tenant.setCreateTime(new Timestamp(DateUtil.current()));
        tenant.setIsDelete(0);
        tenantMapper.insert(tenant);

        // 添加角色信息
        Role role = new Role();
        role.setId(roleId);
        role.setTenantId(tenantId);
        role.setName("超级管理员");
        role.setCreateEmployee(employeeId);
        role.setCreateTime(new Timestamp(DateUtil.current()));
        role.setIsRoot(1);
        role.setIsDelete(0);
        roleMapper.insert(role);

        // 验证登录名是否存在
        if(employeeService.CheckLoginNameIsExist(dto.getEmployeeInfoDto().getLoginName())) {
            throw new Precondition412Exception("loginName.exist","登录名已存在");
        }
        // 添加员工信息
        Employee employee = new Employee();
        employee.setId(employeeId);
        employee.setTenantId(tenantId);
        employee.setIsRoot(1);
        employee.setName(dto.getEmployeeInfoDto().getName());
        employee.setLoginName(dto.getEmployeeInfoDto().getLoginName());
        employee.setLoginPassword(dto.getEmployeeInfoDto().getLoginPassword());
        employee.setPhone(dto.getEmployeeInfoDto().getPhone());
        employee.setEmail(dto.getEmployeeInfoDto().getEmail());
        employee.setUpdateEmployee(employeeId);
        employee.setCreateTime(new Timestamp(DateUtil.current()));
        employee.setIsDelete(0);
        employeeMapper.insert(employee);

        // 添加员工角色关系信息
        EmployeeRole employeeRole=new EmployeeRole();
        employeeRole.setId(UUID.randomUUID().toString());
        employeeRole.setTenantId(tenantId);
        employeeRole.setRoleId(roleId);
        employeeRole.setEmployeeId(employeeId);
        employeeRole.setCreateEmployee(employeeId);
        employeeRole.setCreateTime(new Timestamp(DateUtil.current()));
        employeeRole.setIsDelete(0);
        employeeRoleMapper.insert(employeeRole);

        return tenantId;
    }
}
